<?php

class Trends_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "mis_trends";
        $this->load->model("functions_model");
    }

    function newTrend($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function getSbuClients($sbu) {
        $query = $this->db->query("SELECT DISTINCT client_name FROM mis_trends WHERE SBU='$sbu'");
        $result = array();
        foreach ($query->result_array() as $row) {
            /*  $row['date_prequalified'] = $this->functions_model->prettyDate($row['date_prequalified']);
              $row['engagement_from'] = $this->functions_model->prettyDate($row['engagement_from']);
              $row['engagement_to'] = $this->functions_model->prettyDate($row['engagement_to']); */
            $row['client_id'] = $row['client_name'];
            $list = $this->consolidate_model->getClients($row['client_name']);
            if (count($list) > 0) {
                $row['client_name'] = $list[0]->name;
            }
            /*  $list = $this->consolidate_model->getDepartments($row['SBU']);
              if (count($list) > 0) {
              $row['SBU'] = $list[0]->name;
              }
              $list = $this->consolidate_model->getServiceTypes($row['service_type']);
              if (count($list) > 0) {
              $row['service_type'] = $list[0]->name;
              }
              $tid = $row['tid'];
              $row['more'] = "<a href='javascript:loadUrl(\"#trends_result\",\"" . base_url() . "index.php?/more_info/trendsMoreInfo/" . $row['tid'] . "\")'>More details</a>  <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteTrend/$tid\",\"Are you sure to delete this record?\")'>Delete</a> "; */
            $result[] = $row;
        }
        return $result;
    }

    function getAllMini($username=NULL, $sbu=NULL, $client=NULL) {
        if ($username) {
            $data['added_by'] = $username;
        }
        if ($sbu) {
            $data['SBU'] = $sbu;
        }
        if ($client) {
            $data['client_name'] = $client;
        }
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_prequalified'] = $this->functions_model->prettyDate($row['date_prequalified']);
            $row['engagement_from'] = $this->functions_model->prettyDate($row['engagement_from']);
            $row['engagement_to'] = $this->functions_model->prettyDate($row['engagement_to']);
            $row['client_id'] = $row['client_name'];
            $list = $this->consolidate_model->getClients($row['client_name']);
            if (count($list) > 0) {
                $row['client_name'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getDepartments($row['SBU']);
            if (count($list) > 0) {
                $row['SBU'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getServiceTypes($row['service_type']);
            if (count($list) > 0) {
                $row['service_type'] = $list[0]->name;
            }
            $tid = $row['tid'];
            $row['more'] = "<a href='javascript:loadUrl(\"#trends_result\",\"" . base_url() . "index.php?/more_info/trendsMoreInfo/" . $row['tid'] . "\")'>More details</a>  ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].=" <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteTrend/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            $result[] = $row;
        }
        return $result;
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select("client_name, SBU,service_type,business_volume,date_prequalified,engagement_from,added_by,date_added,engagement_to");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_prequalified'] = $this->functions_model->prettyDate($row['date_prequalified']);
            $row['engagement_from'] = $this->functions_model->prettyDate($row['engagement_from']);
            $row['engagement_to'] = $this->functions_model->prettyDate($row['engagement_to']);
            $list = $this->consolidate_model->getClients($row['client_name']);
            if (count($list) > 0) {
                $row['client_name'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getDepartments($row['SBU']);
            if (count($list) > 0) {
                $row['SBU'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getServiceTypes($row['service_type']);
            if (count($list) > 0) {
                $row['service_type'] = $list[0]->name;
            }
            $result[] = $row;
        }
        return $result;
    }

//    function getAll($username) {
//      //  $data['added_by'] = $username;
//        $this->db->select("tid,client_name, SBU,service_type,date_prequalified,engagement_from,engagement_to");
//        //$this->db->where($data);
//        $this->db->from($this->tableName);
//        $this->db->order_by("tid", "desc");
//        $result = array();
//        $query = $this->db->get();
//        foreach ($query->result_array() as $row) {
//            $row['date_prequalified'] = $this->functions_model->prettyDate($row['date_prequalified']);
//            $row['engagement_from'] = $this->functions_model->prettyDate($row['engagement_from']);
//            $row['engagement_to'] = $this->functions_model->prettyDate($row['engagement_to']);
//            $row['more'] = "<a href='javascript:loadUrl(\"#trends_result\",\"" . base_url() . "index.php?/more_info/trendsMoreInfo/" . $row['tid'] . "\")'>More details</a>";
//            $list = $this->consolidate_model->getClients($row['client_name']);
//            if (count($list) > 0) {
//                $row['client_name'] = $list[0]->name;
//            }
//            $list = $this->consolidate_model->getDepartments($row['SBU']);
//            if (count($list) > 0) {
//                $row['SBU'] = $list[0]->name;
//            }
//            $list = $this->consolidate_model->getServiceTypes($row['service_type']);
//            if (count($list) > 0) {
//                $row['service_type'] = $list[0]->name;
//            }
//            unset ($row['tid']);
//            $result[] = $row;
//        }
//        return $result;
//    }

    function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            $data[$field] = $search;
            $this->db->like($data);
        }
        $this->db->select("tid, date_added, added_by, client_name, SBU,service_type,date_prequalified,engagement_from,engagement_to,date_added");
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $added_by = $row['added_by'];
            unset($row['tid']);
            $row['added_by'] = "<a href='javascript:loadUrl(\"#trend_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
            $row['engagement_from'] = $this->functions_model->prettyDate($row['engagement_from']);
            $row['engagement_to'] = $this->functions_model->prettyDate($row['engagement_to']);
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $list = $this->consolidate_model->getClients($row['client_name']);
            if (count($list) > 0) {
                $row['client_name'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getDepartments($row['SBU']);
            if (count($list) > 0) {
                $row['SBU'] = $list[0]->name;
            }
            $list = $this->consolidate_model->getServiceTypes($row['service_type']);
            if (count($list) > 0) {
                $row['service_type'] = $list[0]->name;
            }
            $row['more'] = "<a href='javascript:loadUrl(\"#trends_result\",\"" . base_url() . "index.php?/more_info/trendsMoreInfo/" . $row['tid'] . "\")'>More details</a>  ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].="  <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteTrend/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            $result[] = $row;
        }
        return $result;
    }

}

?>
